import fs from "fs"
import path, { basename } from "path"

import { safeParse } from "valibot"

import { registry } from "../registry"
import { registrySchema } from "../registry/schema"

const REGISTRY_PATH = path.join(process.cwd(), "public/registry")

const result = safeParse(registrySchema, registry)
if (!result.success) {
  console.error(result.issues)
  process.exit(1)
}

// #######################################
//    BUILD __registry__/index.tsx.
// #######################################

let index = `
// @ts-nocheck
// This file is autogenerated by scripts/build-registry.ts
// Do not edit this file directly.
import { lazy } from "solid-js"

import type { RegistryIndex } from "~/registry/schema"

export const Index: RegistryIndex = {
`
for (const item of result.output) {
  index += `
  "${item.name}": {
    name: "${item.name}",
    description: "${item.description ?? ""}",
    type: "${item.type}",
    registryDependencies: ${JSON.stringify(item.registryDependencies)},
    component: lazy(() => import("~/registry/${item.type}/${item.name}")),
    files: [${item.files.map(
      (file) => `{
      path: "registry/${file.path}",
      type: "${file.type}",
      target: "${file.target ?? ""}"
    }`
    )}],
  },`
}
index += `
}
`

fs.writeFileSync(path.join(process.cwd(), "src", "__registry__", "index.tsx"), index)

// #######################################
//    BUILD registry/ui/[name].json
// #######################################

for (const item of result.output) {
  if (item.type !== "ui") {
    continue
  }

  const files = item.files?.map((file) => {
    const content = fs
      .readFileSync(path.join(process.cwd(), "src", "registry", file.path), "utf8")
      .replaceAll("\r\n", "\n")

    return {
      name: basename(file.path),
      content
    }
  })

  const payload = {
    ...item,
    files
  }

  fs.writeFileSync(
    path.join(REGISTRY_PATH, item.type, `${item.name}.json`),
    JSON.stringify(payload, null, 2),
    "utf8"
  )
}

// #######################################
//    BUILD registry/index.json
// #######################################

const uiPayload = result.output
  .filter((item) => item.type === "ui")
  .map((item) => ({
    ...item,
    files: item.files.map((file) => file.path)
  }))

fs.writeFileSync(
  path.join(REGISTRY_PATH, "index.json"),
  JSON.stringify(uiPayload, null, 2),
  "utf-8"
)
